package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.pojo.Article;
import com.itheima.service.ArticleService;
import com.itheima.utile.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;
    @Autowired
    IdWorker idWorker;
    @GetMapping("/{id}")
    public Result get(@PathVariable("id") String id){
        Article article = articleService.get(id);
        return new Result(true, StatusCode.OK,"查询成功",article);
    }
    @PostMapping
    public Result add(@RequestBody Article article){
        long l = idWorker.nextId();
        article.setId(String.valueOf(l));
        articleService.add(article,idWorker);
        return new Result(true, StatusCode.OK,"添加成功");
    }
    //文章订阅  用户点击订阅按钮，传递用户id和文章作者id
    @PostMapping("/subscribe")
    public Result subscribe(@RequestBody Map<String,String> map){
        Boolean b  = articleService.subscribe(map);
        if(b){
            return new Result(true, StatusCode.OK,"订阅成功");
        }else{
            return new Result(true, StatusCode.OK,"订阅取消");
        }
    }
    //文章点赞  根据用户id和文章id点赞
    @PostMapping("/thumbup")
    public Result thumbup(@RequestBody Map<String,String> map){
        Boolean isthumbup =  articleService.thumbup(map);
        if(isthumbup){
            return new Result(true,StatusCode.OK,"点赞成功");
        }else{
            return new Result(true,StatusCode.OK,"不能重复点赞");
        }
    }


}
